<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Liste extends CI_Controller {
	private  $currentyear;
function __construct()
    {
        parent::__construct();
    	$this->load->library('session');
		$this->load->helper('url');
    	$this->load->database();
    	$this->load->model("centermod");
    	$this->currentyear=date("Y");
    	
    	
	}


	
//################### section load page##################
/*
 * add new tour
 * 
 * */	
	function listtour($year=0)
			{
			//$this->session->unset_userdata('user');
		if($this->session->userdata("user")==false)
			{
					header('Location:index.php?pages/loginadmin');
						
			}else 
			{
				$this->centermod->deletenoinvoice();
				$level=$this->session->userdata("level");
				if($year==0)
				{
					$year=$this->currentyear;
				}
	/*list years */			
				$lyear=$this->centermod->ltouryear();
				//print_r($lyear);
	/*list tours order by date*/			
				$ltours=$this->centermod->ltourbyyear($year);
				//print_r($ltours);
				$yearselect=$year;
				
				//add lsbus for very tours
				$listbus=array();
				foreach ($ltours as $lt)
				{
					$idtour=$lt["idtours"];
					$listbusinfo=$this->centermod->lbus($idtour);
					$listbus["$idtour"]=$listbusinfo;
					
				}
				
				$data=array("level"=>$level,"lyear"=>$lyear,"ltours"=>$ltours,"selectyear"=>$yearselect,"listbus"=>$listbus);
				$this->load->view("tourlist",$data);
				
			}//no login
			}//function page listtour
			
	/*list le chambre information by idtour*/
			function getlcountchambre($idtour)
			{
				$listbusinfo=$this->centermod->lbus($idtour);
				foreach ($listbusinfo as $lbus)
				{
					$idbus=$lbus["idbus"];
					$ldetailchambre=$this->centermod->countchambre($idbus);
					for($i=1;$i<=4;$i++)
					{
						$lchambre["$i"]=0;
						foreach ($ldetailchambre as $lc)
						{
							if($i==$lc["chambre"])
							{
								$lchambre["$i"]=$lc["total"];
								
							}
						}
						
					}
					$lchambrebybus["$idbus"]=$lchambre;
				}
				
				echo json_encode($lchambrebybus); 
			
			}	
/* get place info*/
			function getplaceinfo($idtours)
			{
				 $listbus=$this->centermod->lbus($idtours);
				 foreach($listbus as $lb)
				 {
				$this->centermod->sustactionper($lb["idbus"]);
				 }	
				$placeinfo=$this->centermod->getplaceinfo($idtours);
							
				foreach ($placeinfo as $pi)
				{
					$returnarr[$pi["idbus"]]["seatqty"]=$pi["seatqty"];
					$returnarr[$pi["idbus"]]["seatavaible"]=$pi["seatavaible"];
				}
				
				echo json_encode($returnarr); 
			}			
	
/*list detail*/			
		function listdetail($idbus,$idtour)
			{
			//$this->session->unset_userdata('user');
		if($this->session->userdata("user")==false)
			{
					header('Location:index.php?pages/loginadmin');
						
			}else 
			{
				$level=$this->session->userdata("level");
				$linfobus=$this->centermod->linfodetail($idbus,$idtour);
				$lclientinbus=$this->centermod->lclientdetail($idbus,$idtour);
				$lgroup_person_infor=$this->centermod->getclientgourpid($idbus,$idtour);
				$listgroup=array();
				foreach ($lgroup_person_infor as $lpi)
				{
					
					$lgroupelement["person"]=$lpi;
					$lgroup_chmabre_infor=$this->centermod->lclientdetailbygourp($idbus,$idtour,$lpi["fk_idclients"]);
					$lgroupelement["chambre"]=$lgroup_chmabre_infor;
					$listgroup[]=$lgroupelement;
					}
				$date=array("linfobus"=>$linfobus,"lclientinbus"=>$lclientinbus,"lgroup"=>$listgroup,"idbus"=>$idbus,"idtour"=>$idtour,"level"=>$level);
				$this->load->view("listdetail",$date);
				
			}//no login
			}//function page listtour		

			
//ckplacevalid
function ckplacevalid($idbus)
{
	$placevalid=$this->centermod->getvalidplace($idbus);
		
				echo json_encode($placevalid[0]["seatavaible"]);
}

function viewinvoice()
	{
				//$this->session->unset_userdata('user');
		if($this->session->userdata("user")==false)
			{
					header('Location:index.php?pages/loginadmin');
						
			}else 
			{
		$idinvoice=$this->input->get_post("idinvoice");
		$idclient=$this->input->get_post("idclient");
		$idinscriptions=$this->input->get_post("idinscriptions");;
		$linfoper=$this->centermod->seachclientbyid($idclient);
      	$infoallforinvoice=$this->centermod->allinfoforinvoice($idinscriptions);
      	 $data_invoice["infoper"]=$linfoper;
      	$data_invoice["allinfo"]=$infoallforinvoice;
     	 $data_invoice["idinvoice"]=$idinvoice;
     	 $data_invoice["from"]="listdetail";
    	 $this->load->view("invoices",$data_invoice);
			}
	}
	
	
function viewinvoicegrp()
	{
		$idinvoice=$this->input->get_post("idinvoice");
		$idclient=$this->input->get_post("idclient");
		$idinscriptions=$this->input->get_post("idinscriptions");
		$id_inscr_arr=explode(",", $idinscriptions);
		
				foreach($id_inscr_arr as $idinscr)
					{
      					$infoallforinvoice[]=$this->centermod->allinfoforinvoice($idinscr);
      				}
      	$linfoper=$this->centermod->seachclientbyid($idclient);			  		
		
		
		
		$linfoper=$this->centermod->seachclientbyid($idclient);
      	$data_invoice["idinvoice"]=$idinvoice;	
	 	$data_invoice["infoper"]=$linfoper;
     	$data_invoice["allinfo"]=$infoallforinvoice;
     	 $data_invoice["idinvoice"]=$idinvoice;
     	 $data_invoice["from"]="listdetail";
     	$this->load->view("invoicesgroup",$data_invoice);
     	
	}	
	
// liste user
function  listuser()
	{
				//$this->session->unset_userdata('user');
		if($this->session->userdata("user")==false ||$this->session->userdata("level")!="admin")
			{
					header('Location:index.php?pages/loginadmin');
						
			}else 
			{
				$listusers=$this->centermod->getusers();
				$data=array("listusers"=>$listusers);
				$this->load->view("partners/listuser",$data);
				
				
			}
	}
	
/****************** party for partner module   ************************/
				//$this->session->unset_userdata('user')
	function  listpartners()
	{
		if($this->session->userdata("user")==false ||$this->session->userdata("level")!="admin")
			{
					header('Location:index.php?pages/loginadmin');
						
			}else 
			{
				$listpartners=$this->centermod->getpartners();
				$data=array("listpartners"=>$listpartners);
				$this->load->view("partners/listpartners",$data);
	       }
	}
/*********************************************************************/
	
	
}//class
